// 1 引入express 
var express = require("express");
var app = express();
var path = require("path")
var bodyParser = require("body-parser")
// 2. 解决cors跨域
app.use('*', function (req, res, next) {
  // res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:8020');
  res.setHeader('Access-Control-Allow-Origin', '*')//允许哪些请求地址 
  res.setHeader('Access-Control-Allow-Methods', '*')//允许哪些请求方式 get post put delete 
  res.setHeader('Access-Control-Allow-Headers', 'content-type')//允许哪些请求头
  next()//继续执行
})
//3.配置静态资源路径
app.use(express.static(path.join(__dirname, "public")))

// 4  解决post请求解析问题
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
// 5.引入路由
var indexRouter = require("./routes/index")
var userRouter = require("./routes/users")
//6.使用路由
app.use("/", indexRouter)
app.use("/users", userRouter)
//7.设置服务器的监听ip和端口
app.listen(3009, "127.0.0.1", function (params) {
  console.log("服务器启动成功")
})
